﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Handrise.BasicModal.Serialization;
using Handrise.CDCable.Business;

namespace Handrise.CDCable.WebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld(byte[] test)
        {
            //CompactFormatterPlus cfp = new CompactFormatterPlus();
            //Line line1 = (Line)cfp.Deserialize(test);
            return "Hello World";
        }

        /// <summary>
        /// 下载标准数据和设备数据
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="deviceUpdateDateTime">最后一次更新设备的时间</param>
        /// <returns></returns>
        [WebMethod]
        public string DownloadStandardDataAndDevice(string userName,string deviceUpdateDateTime)
        {
            try
            {
                ///下载标准数据和设备
                StandardDataAndDeviceSynchronize standardDataAndDeviceSynchronize = new StandardDataAndDeviceSynchronize();
                return standardDataAndDeviceSynchronize.DownloadStandardDataAndDevice(userName, deviceUpdateDateTime);
            }
            catch (Exception)
            {
                return "Error";
            }
        }

        /// <summary>
        /// 同步设备
        /// </summary>
        /// <param name="tmpFileID">临时存放的设备文件ID</param>
        /// <param name="devicesBuffer">需要返回给PDA的设备通用对象列表流</param>
        /// <param name="deviceDetailsBuffer">需要返回给PDA的设备详情对象列表流</param>
        /// <returns></returns>
        [WebMethod]
        public bool SynchronizeDevice(string tmpFileID, int beginIndex, out byte[] devicesBuffer, out byte[] deviceDetailsBuffer)
        {
            devicesBuffer = null;
            deviceDetailsBuffer = null;
            try
            {
                StandardDataAndDeviceSynchronize deviceSynchronize = new StandardDataAndDeviceSynchronize();
                deviceSynchronize.SynchronizeDevice(tmpFileID, out devicesBuffer, out deviceDetailsBuffer, beginIndex);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 同步标准数据
        /// </summary>
        /// <param name="tmpFileID"></param>
        /// <param name="beginIndex"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool SynchronizeStandardData(string tmpFileID, int beginIndex, out byte[] buffer)
        {
            buffer = null;
            try
            {
                StandardDataAndDeviceSynchronize standardDataSynchronize = new StandardDataAndDeviceSynchronize();
                standardDataSynchronize.SynchronizeStandardData(tmpFileID, out buffer, beginIndex);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 同步GIS数据
        /// </summary>
        /// <param name="tmpFileID"></param>
        /// <param name="beginIndex"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool SynchronizeDeviceGPSData(string tmpFileID, int beginIndex, out byte[] buffer)
        {
            buffer = null;
            try
            {
                StandardDataAndDeviceSynchronize standardDataSynchronize = new StandardDataAndDeviceSynchronize();
                standardDataSynchronize.SynchronizeDeviceGPSData(tmpFileID, out buffer, beginIndex);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="buffer">返回的用户实体序列化流</param>
        /// <returns>登录成功true,失败false</returns>
        [WebMethod]
        public bool Login(string userName, string password, out byte[] buffer)
        {
            buffer = null;
            try
            {
                PermitService permitService = new PermitService();
                return permitService.Login(userName, password,out buffer);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 下载巡视计划（计划、计划设备、缺陷、指挥信息）
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="plansBuffer">返回的巡视计划实体序列化流</param>
        /// <param name="planDevicesBuffer">返回的巡视设备实体序列化流</param>
        /// <param name="flawsBuffer">返回的缺陷实体序列化流</param>
        /// <param name="communicationsBuffer">返回的指挥信息实体序列化流</param>
        /// <returns></returns>
        [WebMethod]
        public bool DownloadPlan(string userName, out byte[] plansBuffer, out byte[] planDevicesBuffer, out byte[] flawsBuffer, out byte[] communicationsBuffer)
        {
            plansBuffer = null;
            planDevicesBuffer = null;
            flawsBuffer = null;
            communicationsBuffer = null;
            try
            {
                PlanService planService = new PlanService();
                return planService.DownloadPlan(userName, out plansBuffer, out planDevicesBuffer, out flawsBuffer, out communicationsBuffer);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 确认下载巡视计划完成
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="planIDs">下载的巡视计划ID，多个通过“,”分隔</param>
        /// <param name="communicationIDs">下载的指挥信息，多个通过“,”分隔</param>
        /// <returns></returns>
        [WebMethod]
        public bool ComfireDownloadPlan(string userName, string planIDs, string communicationIDs)
        {
            try
            {
                PlanService planService = new PlanService();
                return planService.ComfireDownloadPlan(userName, planIDs, communicationIDs);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 上传计划状态
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="plansBuffer">计划实体序列化流</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadPlanState(string userName,byte[] plansBuffer)
        {
            try
            {
                PlanService planService = new PlanService();
                return planService.UploadPlanState(userName, plansBuffer);
            }
            catch (Exception e)
            {
                
                return false ;
            }
        }

        /// <summary>
        /// 上传缺陷
        /// </summary>
        /// <param name="flawsBuffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadFlaw(string userName,byte[] flawsBuffer)
        {
            try
            {
                FlawService flawSerivce = new FlawService();
                return flawSerivce.UploadFlaws(userName,flawsBuffer);
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 上传轨迹跟踪信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="gpsTracesBuffer">轨迹跟踪实体列表序列化流</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadGPSTrace(string userName, byte[] gpsTracesBuffer)
        {
            try
            {
                GPSTraceService gpsTraceService = new GPSTraceService();
                return gpsTraceService.UploadGPSTrace(userName, gpsTracesBuffer);
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 上传指挥信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="communicationsBuffer">指挥信息实体列序列化流</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadCommunication(string userName, byte[] communicationsBuffer)
        {
            try
            {
                CommunicationService communicationService = new CommunicationService();
                return communicationService.UploadCommunication(userName, communicationsBuffer);
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 上传测温信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="tempraturesBuffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadTemprature(string userName, byte[] tempraturesBuffer)
        {
            try
            {
                TempratureService tempratureService = new TempratureService();
                return tempratureService.UploadTemprature(userName, tempraturesBuffer);
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 上传巡视记录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="patrolLogsBuffer">巡视日志实体列表序列化流</param>
        /// <param name="patrolDetailsBuffer">巡视详情实体列表序列化流</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadPatrolInfo(string userName, byte[] patrolLogsBuffer, byte[] patrolDetailsBuffer)
        {
            try
            {
                PatrolInfoService PatrolInfoService = new PatrolInfoService();
                return PatrolInfoService.UploadPatrolInfo(userName, patrolLogsBuffer, patrolDetailsBuffer);
            }
            catch (Exception e)
            {

                return false;
            }
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="fileInfoBuffer">文件信息序列化流</param>
        /// <param name="fileBuffer">文件二进制流数据</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadFile(string userName, byte[] fileInfoBuffer, byte[] fileBuffer)
        {
            try
            {
                LinkFileService  linkFileService = new LinkFileService();
                return linkFileService.UploadFile(userName,fileInfoBuffer,fileBuffer) ;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取服务器时间
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public DateTime GetServerDateTime()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// 下载指挥信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="communicationsBuffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool DownloadCommunication(string userName,out byte[] communicationsBuffer)
        {
            communicationsBuffer = null;
            try
            {
                CommunicationService communicationService = new CommunicationService();
                return communicationService.DownloadCommunication(userName,out communicationsBuffer);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 查询是否需要升级
        /// </summary>
        /// <param name="clientVersion">客户端版本号</param>
        /// <returns></returns>
        [WebMethod]
        public bool QueryNeedUpdate(string clientVersion, out long fileLen, out string serverVersion)
        {
            fileLen = 0;
            serverVersion = "";
            try
            {
                UpdateService updateService = new UpdateService();
                return updateService.GetNeedUpdate(clientVersion, out fileLen, out serverVersion);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 同步升级数据
        /// </summary>
        /// <param name="beginIndex"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool SynchronizeUpdateData(int beginIndex, out byte[] buffer)
        {
            buffer = null;
            try
            {
                UpdateService updateService = new UpdateService();
                updateService.SynchronizeUpdateData(beginIndex, out buffer);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取地图数据的大小
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public long GetGISDataLen()
        {
            try
            {
                GISService gisService = new GISService();
                return gisService.GetGISDataLen();
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        /// <summary>
        /// 同步GIS数据
        /// </summary>
        /// <param name="beginIndex"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        [WebMethod]
        public bool SynchronizeGISData(int beginIndex, out byte[] buffer)
        {
            buffer = null;
            try
            {
                GISService gisService = new GISService();
                gisService.SynchronizeGISData(beginIndex, out buffer);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }


    }
}
